package com.example.firstapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    private EditText et_main_message;
    private Button bt_main_start1;
    private Button bt_main_start2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: " );
        setContentView(R.layout.activity_main);


        //初始化视图对象
        et_main_message=(EditText)findViewById(R.id.et_main_message);
        this.bt_main_start1 = (Button)findViewById(R.id.btn_main_start1);
        this.bt_main_start2=(Button)findViewById(R.id.btn_main_start2);

        this.bt_main_start1.setOnClickListener(this);
        this.bt_main_start2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_main_start1:{
                Intent intent = new Intent(this,SecondActivity.class);
                startActivity(intent);
                break;
            }
            case R.id.btn_main_start2:{
                Intent intent = new Intent(this,SecondActivity.class);
                intent.putExtra("MESSAGE",this.et_main_message.getText().toString());
                startActivityForResult(intent,2);
                break;
            }

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //判断code
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && resultCode == 3) {
            String result = data.getStringExtra("RESULT");
            this.et_main_message.setText(result);
        }
        //从data中取出数据
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: " );
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: " );
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: " );
    }

    @Override
    protected void onStop() {
        Log.e(TAG, "onStop: " );
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.e(TAG, "onDestroy: " );
        super.onDestroy();
    }
}
